package com.own.business.system.user.controller.user;

import com.own.aop.login.LoginIgnore;
import com.own.business.system.user.business.service.SystemRegisterService;
import com.own.business.system.user.entity.form.RegisterForm;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.component.log.operation.aop.LogOperationAop;
import com.own.component.store.core.util.base.RsaKeyUtil;
import com.own.constant.ConstantApiPrefix;
import com.own.constant.ConstantBeanSuffix;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * UserSystemRegisterController
 *
 * @author chenxueli
 * @date 2023-08-09 15:50:19
 */
@Tag(name = "注册模块" + ConstantBeanSuffix.USER_CLIENT + ConstantBeanSuffix.CONTROLLER)
@RestController
@RequestMapping(ConstantApiPrefix.BASE_API_V1_PREFIX + "user/system/register")
public class UserSystemRegisterController extends BaseController {

    @Resource
    private SystemRegisterService systemRegisterService;

    @Resource
    private RsaKeyUtil rsaKeyUtil;

    @LoginIgnore
    @LogOperationAop
    @Operation(summary = "注册")
    @PostMapping("/")
    public ResultModel<Boolean> register(
            @RequestBody RegisterForm form
    ) {
        form.check();
        // 如果没有key，表示为不加密注册
        if (StringUtils.isNotBlank(form.getKey())) {
            var password = rsaKeyUtil.decrypt(form.getKey(), form.getPassword());
            form.setPassword(password);
        }
        systemRegisterService.register(form.getUsername(), form.getPassword(), form.getType(), form.getAccountType());
        return R.success(true, "注册成功");
    }

}
